package two_shang._11_09;

import java.util.HashSet;

public class Student {
    private String stuId;
    private String name;
    private float score;

    public Student(String stuId, String name, float score) {
        this.stuId = stuId;
        this.name = name;
        this.score = score;
    }

    // Getters and Setters


    public String getStuId() {
        return stuId;
    }

    public void setStuId(String stuId) {
        this.stuId = stuId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getScore() {
        return score;
    }

    public void setScore(float score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "学生 [stuId=" + stuId + ", name=" + name + ", score=" + score + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (int) score;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null || getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        //写的没错，根本不用if
        return stuId == other.stuId && name.equals(other.name);
    }

    public static void main(String[] args) {
        HashSet<Student> studentSet = new HashSet<>();

        // 创建三个学生对象并添加到HashSet中
        Student student1 = new Student("1", "爱丽丝", 90.5f);
        Student student2 = new Student("3221306130", "李学健", 85.0f);
        Student student3 = new Student("1", "爱丽丝", 90.5f); // 重复的学生对象

        studentSet.add(student1);
        studentSet.add(student2);
        studentSet.add(student3);

        // 显示HashSet中元素的内容
        System.out.println("HashSet中的学生信息：");
        for (Student student : studentSet) {
            System.out.println(student);
        }
    }
}
